package org.scohen.juploadr.upload;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.scohen.juploadr.prefs.Configuration;

public class BandwidthLimitingProtocolSocketFactory extends
        DefaultProtocolSocketFactory implements ProtocolSocketFactory {
    private Configuration config = Configuration.getInstance();

    public Socket createSocket(String host, int port, InetAddress localAddress,
            int localPort) throws IOException, UnknownHostException {
        return new BandwidthThrottlingSocket(super.createSocket(host, port,
                localAddress, localPort), config.getMaxKPerSecond());
    }

    public Socket createSocket(String host, int port, InetAddress localAddress,
            int localPort, HttpConnectionParams params) throws IOException,
            UnknownHostException, ConnectTimeoutException {
        return new BandwidthThrottlingSocket(super.createSocket(host, port,
                localAddress, localPort), config.getMaxKPerSecond());
    }

    public Socket createSocket(String host, int port) throws IOException,
            UnknownHostException {
        return new BandwidthThrottlingSocket(super.createSocket(host, port),
                config.getMaxKPerSecond());
    }

}
